package com.example.demo.程序员面试金典;

/**
 * @description: TODO
 * @author: zhangzc
 * @modified By: zhangzc
 * @date: Created in 2022/9/8 15:50
 * @version:v1.0
 */
public class 颜色填充 {

	// todo
	class Solution {
		private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

		public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
			dfs(image, sr, sc, image[sr][sc], newColor);
			return image;
		}

		private void dfs(int[][] image, int i, int j, int oc, int nc) {
			if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
				return;
			}
			image[i][j] = nc;
			for (int[] dir : dirs) {
				dfs(image, i + dir[0], j + dir[1], oc, nc);
			}
		}
	}


}
